package com.wisdom.tools;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.os.StrictMode;
 
/**
 * HttpUtil Class Capsule Most Functions of Http Operations
 * 
 * @author sfshine
 * 
 */ 
public class HttpUtil { 
    public static JSONObject post(String url,JSONObject json){
    	HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		StreamToString test = new StreamToString();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

		StrictMode.setThreadPolicy(policy); 
		String key=CCPConfig.Src_phone;
		String secret=CCPConfig.Password;
		((DefaultHttpClient) client).getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),new UsernamePasswordCredentials(key,secret));
		try {
			
	        post.addHeader("Content-Type", "application/json"); 
			post.addHeader("Accept","application/json");
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);
				 response =new JSONObject(result);  
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return response;
    	
    }
    public static String postReturnStr(String url,JSONObject json){
    	HttpClient client = new DefaultHttpClient();
    	HttpPost post = new HttpPost(url);
    	StreamToString test = new StreamToString();
    	String result = "";
    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    	
    	StrictMode.setThreadPolicy(policy); 
    	String key=CCPConfig.Src_phone;
    	String secret=CCPConfig.Password;
    	((DefaultHttpClient) client).getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),new UsernamePasswordCredentials(key,secret));
    	try {
    		
    		post.addHeader("Content-Type", "application/json"); 
    		post.addHeader("Accept","application/json");
    		StringEntity s = new StringEntity(json.toString());
    		s.setContentEncoding("UTF-8");
    		s.setContentType("application/json");
    		post.setEntity(s);
    		HttpResponse res = client.execute(post);
    		
    		if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
    			HttpEntity entity = res.getEntity();
    			result = EntityUtils.toString(entity);
    		}
    	} catch (Exception e) {
    		throw new RuntimeException(e);
    	}
    	return result;
    	
    }
    
} 
